/*
 * @(#)FileRename.java		       version: 0.1 
 * Date:2012-2-22
 *
 * Copyright (c) 2011 CFuture09, Institute of Software, 
 * Guangdong Ocean University, Zhanjiang, GuangDong, China.
 * All rights reserved.
 */
package com.sinaapp.msdxblog.tools.xiaoxiee;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;

/**
 * @author Geek_Soledad (66704238@51uc.com)
 */
public class GenTextAndRename {

	public static void main(String[] args) {
		String path = "E:/androidApk/2012/有内涵工作室/app/小邪恶大全/2";
		genText(renameFiles(path));
	}

	public static List<Res> renameFiles(String dirPath) {
		File dir = new File(dirPath);
		if (!dir.isDirectory()) {
			return null;
		}
		List<Res> reses = new ArrayList<Res>();
		File[] files = dir.listFiles();
		for (int i = 0, length = files.length; i < length; i++) {
			String ext = files[i].getName().substring(
					files[i].getName().lastIndexOf("."));
			File file = new File(dir + File.separator + i + ext);
			System.out.println(file.getAbsolutePath());
			System.out.println(trimExt(files[i].getName()));
			files[i].renameTo(file);
			reses.add(new Res(trimExt(files[i].getName()), file.getName()));
		}
		return reses;
	}

	public static void genText(List<Res> reses) {
		List<String> lines = new ArrayList<String>();
		for (Res res : reses) {
			lines.add(res.getFileName() + "=" + res.getShowName());
		}
		try {
			FileUtils.writeLines(new File(
					"E:/androidApk/2012/有内涵工作室/app/小邪恶大全/2/text.txt"), lines);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 去除后缀
	 * 
	 * @param name
	 * @return
	 */
	public static String trimExt(String name) {
		return name.substring(0, name.lastIndexOf("."));
	}
}
